<?php
namespace app\api\components;

class Auth
{
    private static $_instance = NULL;
    private static $_access_code  = "app.com_2015_1_AAA(";//访问密钥
    private static $_encrypt_code = "app.com_2015_1_!AAA%";//用户密钥

    public static function getInstance()
    {
        if (self::$_instance === NULL) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    public function VerifyAuth($member_info){
        $member_info = json_decode(stripslashes( htmlspecialchars_decode($member_info)),true);
        if(!$this->verifyToken($member_info,$member_info['token'])){
            return false;
        }

        return $member_info;
    }

    /*
     * 协议输出
     * @param $status 0成功 1失败
     * @param $data array
     * @param $msg 消息
     * return json
     */
    public static function printResult($status=1,$data=array(),$msg='',$total='',$page=''){
        $data = array('code'=>$status,'result'=>$data,'message'=>$msg);
        if($total&&$page){
            $data['total'] =$total;
            $data['page']  =$page;
        }
        return json_encode($data);
    }

    /*
     * 生成用户登陆标识token
     * @param $userExists array 登录用户信息
     * return string
     */
    public function createToken($userExists){
        if (empty($userExists)){
            return false;
        }



        return md5(sha1($userExists['user_id'].$userExists['mobile'].$userExists['create_time'].self::$_encrypt_code));
    }

    /*
     * 验证用户
     *
     */
    public function verifyToken($userExists,$token=''){
        return md5(sha1($userExists['user_id'].$userExists['login_account'].$userExists['create_time'].self::$_encrypt_code)) == $token ? true : false;
    }

    /*
     * 验证合法性
     *
     */
    public function verifyData($validate){
        return self::$_access_code == $validate ? true : false;
    }
}